package com.freesky.biz.activity.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.freesky.biz.activity.domain.TRpaTask;
import com.freesky.biz.activity.mapper.RpaTaskMapper;
import com.freesky.biz.activity.service.IRpaTaskService;
import com.freesky.biz.activity.service.IWebhookService;
import com.freesky.common.core.domain.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Slf4j
public class WebhookServiceImpl implements IWebhookService {

    @Override
    public AjaxResult sendText(String url, String content, List<String> mentionedMobileList) {
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("msgtype","text");

        Map<String,Object> textMap = new HashMap<>();
        textMap.put("content", content);
        if(mentionedMobileList!=null){
            textMap.put("mentioned_mobile_list",mentionedMobileList);
        }
        paramMap.put("text",textMap);

        log.info("【sendText req】" + JSON.toJSONString(paramMap));
        String result = HttpRequest.post(url)
                .header(Header.USER_AGENT, "Chrome")
                .header(Header.CONTENT_TYPE,"application/json")
                .charset("utf8")
                .body(JSON.toJSONString(paramMap))
                .timeout(10000)
                .execute().body();
        log.info("【sendText resp】" + result);

        return AjaxResult.success();
    }

    @Override
    public AjaxResult sendImage(String url, String base64) {
        String BASE64_IMAGE = "data:image/png;base64,";
        base64 = base64.replaceAll(BASE64_IMAGE,"");
        String sign = DigestUtils.md5Hex(Base64.decodeBase64(base64));

        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("msgtype","image");
        Map<String,Object> imageMap = new HashMap<>();
        imageMap.put("base64",base64);
        imageMap.put("md5",sign);
        paramMap.put("image",imageMap);
        //FileUtil.writeBytes(Base64.decodeBase64(base64),new File("/Users/zhengxiansheng/Documents/test.png"));

        log.info("【sendImage req】" + JSON.toJSONString(paramMap));
        String result = HttpRequest.post(url)
                .header(Header.USER_AGENT, "Chrome")
                .header(Header.CONTENT_TYPE,"application/json")
                .charset("utf8")
                .body(JSON.toJSONString(paramMap))
                .timeout(10000)
                .execute().body();
        log.info("【sendImage resp】" + result);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult sendNews(String url,
                               List<Map<String, String>> articleList) {
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("msgtype","news");
        Map<String,Object> newsMap = new HashMap<>();
        newsMap.put("articles",articleList);
        paramMap.put("news",newsMap);
        log.info("【sendNews req】" + JSON.toJSONString(paramMap));
        String result = HttpRequest.post(url)
                .header(Header.USER_AGENT, "Chrome")
                .header(Header.CONTENT_TYPE,"application/json")
                .charset("utf8")
                .body(JSON.toJSONString(paramMap))
                .timeout(10000)
                .execute().body();
        log.info("【sendNews resp】" + result);
        return AjaxResult.success();
    }

}
